package sk.fiit.vppj.eventmanager.gui.administrator;

import java.awt.GridLayout;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sk.fiit.vppj.eventmanager.data.Project;
import sk.fiit.vppj.eventmanager.data.ProjectAdminEval;
import sk.fiit.vppj.eventmanager.gui.judge.Evaluation;

/**
 * Main part of paroject tab in administrator account
 * @author Marek
 *
 */
final class AdminProjectsPanel extends JPanel implements ProjectTable {
	private JPanel tablePanel = new ProjectsTable( this );
	private JPanel projectPanel = null;
	private Project project = null;

	public AdminProjectsPanel() {
		setLayout( new GridLayout( 1, 2 ) );
		add( tablePanel );
		this.projectPanel = new JPanel();
		this.projectPanel.setLayout( new BoxLayout( projectPanel, BoxLayout.PAGE_AXIS ) );
		add( this.projectPanel );
	}

	@Override
	public void showProjectData(Project project) {
		this.project = project;
		this.projectPanel.removeAll();
		refreshProjectPanel();
		this.projectPanel.updateUI();

	}

	private void refreshProjectPanel() {
		StringBuilder body = new StringBuilder( 4000 );
		body.append( "<html>" );
		body.append( "<h2>" );
		body.append( this.project.getEngName() );
		body.append( "</h2><br><b>Category:</b> " );
		body.append( this.project.getCategoryName() );
		body.append( "<br><b>Team project:</b> " );
		if( this.project.isTeamProject() ) {
			body.append( "Yes" );
		}
		else {
			body.append( "No" );
		}
		body.append( "<br><b>English abstract:</b> " );
		body.append( this.project.getEngAbstract() );
		this.projectPanel.add( new JLabel( body.toString() ) );
		showEvaluationsTable();
	}

	private void showEvaluationsTable() {
		List<ProjectAdminEval> list = new Evaluation().getAdminEvaluations( this.project.getId() );
		StringBuilder evalTable = new StringBuilder( 1000 );
		int min = 0;
		evalTable.append( "<html><table border='1'><tr>" );

		for ( ProjectAdminEval projectEval : list ) {

			if( min >= projectEval.getCriteriaId() ) {
				min = 0;
				break;
			}
			min = projectEval.getCriteriaId();

			evalTable.append( "<td>" );
			evalTable.append( projectEval.getCriteriaName() );
			evalTable.append( "( " );
			evalTable.append( projectEval.getMaxPoints() );
			evalTable.append( "pts )</td>" );
		}

		evalTable.append( "<td>Judge</td>" );
		evalTable.append( "</tr>" );

		evalTable.append( getEvaluationList( list ) );
		evalTable.append( "<tr>" );
		evalTable.append( "</tr></table></html>" );
		this.projectPanel.add( new JLabel( evalTable.toString() ) );
	}

	private String getEvaluationList(List<ProjectAdminEval> list) {
		int min = 0;
		StringBuilder evalTable = new StringBuilder( 500 );
		for ( int i = 0; i < list.size(); i++ ) {
			if( list.get( i ).getJudgeName() == null ) {
				break;
			}

			min = list.get( i ).getCriteriaId();
			evalTable.append( "<td>" );
			evalTable.append( list.get( i ).getPoints() );
			evalTable.append( "</td>" );

			if( ( list.size() == ( i + 1 ) ) || min >= list.get( i + 1 ).getCriteriaId() ) {

				evalTable.append( "<td>" );
				evalTable.append( list.get( i ).getJudgeName() );
				evalTable.append( "</td>" );
				evalTable.append( "</tr>" );
				min = 0;
			}
		}
		return evalTable.toString();
	}

}
